package com.ksp.wke.kspa01;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import static com.ksp.com.KSPCOM_BasConst.*;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;

import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ksp.api.KSPAPI_UserAgentUtil;
import com.ksp.api.KSPAPI_UsrAuth;
import com.ksp.dom.KSPDOM_ResDta;
import com.ksp.dom.KSPDOM_User;

import net.minidev.json.JSONArray;


/**
 * 
 * @author anye
 *
 */
@Controller
public class KSPA01Controller {

	private static ConcurrentLinkedQueue<Map<String, Object>> artScaQue=
			new ConcurrentLinkedQueue<>();
	
	private ExecutorService artThdPool = Executors.newCachedThreadPool();
	
	public static ReentrantLock queLock = new ReentrantLock(true);

	@Autowired
	private KSPA01Mapper kspa01Mapper;
	
	@RequestMapping("/art/KSPA01")
	@ResponseBody
	public KSPDOM_ResDta iniPam(ServletRequest request) {
		String wBoaTch = request.getParameter(BOA_TCH);
		String wTopTch = request.getParameter(TOP_TCH);
		String wTtlTch = request.getParameter(TTL_TTL);
		String wConcon = request.getParameter(CON_CON);
		
		KSPDOM_User usrInf = KSPAPI_UsrAuth.getUsrInf(request);
		if (usrInf == null) {
			return KSPDOM_ResDta.buildFail("请先登录");
		}
		String wUsrOam = usrInf.getUsroam();
		
		Map<String, Object> wPamMap = new HashMap<>();
		//wPamMap.put("TTL_TCH", "TCH_IDN");
		wPamMap.put(TTL_TTL, wTtlTch);
		wPamMap.put(CON_CON, wConcon);
		wPamMap.put(BOA_TCH, wBoaTch);
		wPamMap.put(TOP_TCH, wTopTch);
		wPamMap.put(CRT_USR, wUsrOam);
		wPamMap.put(FLG_TOP, "N");
		wPamMap.put(SCA_CNT, "0");
		wPamMap.put(GOD_CNT, "0");
		
		KSPDOM_ResDta wResDta = new KSPDOM_ResDta();
		try {
			wResDta = exeSrv(wPamMap);
		} catch (Exception e) {
			KSPDOM_ResDta.buildFail(e.getMessage());
		}
		
		return wResDta;
	}

	public KSPDOM_ResDta exeSrv(Map<String, Object> pPamMap) {
		artScaQue.add(pPamMap);
		if (!KSPA01InsThd.dealLock.isLocked()) {
			KSPA01InsThd wArtThd = new KSPA01InsThd(artScaQue);
			artThdPool.execute(wArtThd);
		}
		
		return KSPDOM_ResDta.buildSucc("");
	}
}
